home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / motif / oedgeflag.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  9KB  |  340 lines

  1. /*
  2.  * Copyright (c) 1993-94, Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  21.  */
  22. /*----------------------------------------------------------------------------
  23.  *
  24.  * oedgeflag.c : openGL (motif) example showing use of edge flag.
  25.  *
  26.  * Author : Yusuf Attarwala
  27.  *          SGI - Applications
  28.  * Date   : Sep 93
  29.  *
  30.  *    press  left   button to move object 
  31.  *           middle button to turn off edge flaging
  32.  *           right  button to turn on  edge flaging
  33.  *
  34.  *
  35.  *---------------------------------------------------------------------------*/
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <strings.h>
  39.  
  40. #include <Xm/Xm.h> 
  41. #include <Xm/Frame.h>               /* for frame widgets */
  42. #include <Xm/Form.h>               /* for frame widgets */
  43. #include <X11/keysym.h>             /* keyboard translations */
  44. #include <X11/StringDefs.h>
  45.  
  46. #include <GL/gl.h>                  /* gl includes */
  47. #include <GL/glu.h>                 /* utility library includes */
  48. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  49.  
  50. /* function declarations */
  51.  
  52. void 
  53.     createToplevel(void),
  54.     drawScene(void),
  55.     setMatrix(void),
  56.     animation(void),
  57.     exposeCB(Widget,XtPointer,XtPointer),
  58.     resizeCB(Widget,XtPointer,XtPointer),
  59.     initCB(Widget,XtPointer,XtPointer),
  60.     inputCB(Widget,XtPointer,XtPointer);
  61.  
  62.  
  63. /* global variables */
  64.             
  65. Display       *display;             /* current display */
  66. XtAppContext  appContext;           /* X application context */
  67. float         ax,ay,az;             /* angles for animation */
  68.  
  69. Widget        toplevel,       /* toplevel shell */
  70.               glw;            /* current widget */
  71.              
  72. GLXContext    glxc;           /* current glx context */
  73.  
  74. /* coordinates of star */
  75. static GLfloat   starv[10][3] = {3.5,5.0,0.0,
  76.                  2.0,2.0,0.0,
  77.                  5.0,4.0,0.0,
  78.                  8.0,2.0,0.0,
  79.                  6.5,5.0,0.0,
  80.                  8.0,6.0,0.0,
  81.                  6.0,6.0,0.0,
  82.                  5.0,8.0,0.0,
  83.                  4.0,6.0,0.0,
  84.                  2.0,6.0,0.0};
  85.  
  86. #define NTRI 8   /* number of triangles in the star */
  87.  
  88. /* connectivity data */
  89. static int       stari[NTRI][3] = {0,1,2,
  90.                    2,3,4,
  91.                    4,5,6,
  92.                    6,7,8,
  93.                    8,9,0,
  94.                    0,2,8,
  95.                    8,2,6,
  96.                    6,2,4};
  97.  
  98. static GLfloat starcolor[NTRI][3] = {1.0,0.0,0.0,
  99.                      0.0,1.0,0.0,
  100.                      0.0,0.0,1.0,
  101.                      1.0,0.0,1.0,
  102.                      0.0,1.0,1.0,
  103.                      1.0,1.0,0.0,
  104.                      1.0,1.0,1.0,
  105.                      1.0,0.5,0.5};
  106.  
  107.  
  108. int doEdgeFlag = 0;
  109.  
  110. /* edge flags */
  111. static GLboolean stare[NTRI][3] = {TRUE,TRUE,FALSE,
  112.                                    TRUE,TRUE,FALSE,
  113.                                    TRUE,TRUE,FALSE,
  114.                                    TRUE,TRUE,FALSE,
  115.                                    TRUE,TRUE,FALSE,
  116.                                    FALSE,FALSE,FALSE,
  117.                                    FALSE,FALSE,FALSE,
  118.                                    FALSE,FALSE,FALSE};
  119.  
  120. Arg args[20];
  121. int acnt;
  122.  
  123. void 
  124. main(int argc, char** argv)
  125. {
  126.     XtToolkitInitialize();
  127.     appContext = XtCreateApplicationContext();
  128.     display    = XtOpenDisplay(appContext, NULL, "Oedge","oedge",NULL,0,
  129.                               &argc,argv);
  130.     if (!display) {
  131.         printf("%s : Unable to open display\n",argv[0]);
  132.         exit(0);
  133.     }
  134.  
  135.     printf("\n---------------------------------------------\n");
  136.     printf("OpenGL edge flag example \n\n");
  137.     printf("Press:  left   button for animation\n\
  138.         middle button to turn OFF edgeflag (default)\n\
  139.         right  button to turn ON  edgeflag \n");
  140.  
  141.     createToplevel();             /* create widget hierarchy */
  142.     XtAppMainLoop(appContext);    /* loop forever */
  143. }
  144.  
  145.  
  146. void
  147. createToplevel(void)
  148. {
  149.     Widget frame;
  150.  
  151.     acnt = 0;
  152.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  153.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  154.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  155.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  156.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  157.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  158.     toplevel  = XtAppCreateShell("openGL edge","xtext",
  159.                                   applicationShellWidgetClass,
  160.                                   display,args,acnt);
  161.  
  162.     /* create a frame to hold glx widget */
  163.     frame   = XtVaCreateManagedWidget("frame", 
  164.                   xmFrameWidgetClass, toplevel, 
  165.                   NULL);
  166.  
  167.     /* create a double buffer widget, in rgb mode and manage it */
  168.     acnt = 0;
  169.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  170.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  171.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  172.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  173.     XtManageChild(glw);
  174.  
  175.     /* register callbacks */
  176.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  177.  
  178.     /* realize widget hierarchy */
  179.     XtRealizeWidget(toplevel);
  180.  
  181.     /* additional callbacks */
  182.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  183.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  184.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  185.  
  186. }
  187.  
  188. void
  189. drawScene(void)
  190. {
  191.     int i;
  192.     glClearColor(0.0, 0.0, 0.0, 0.0);
  193.     glClear(GL_COLOR_BUFFER_BIT);
  194.  
  195.     glPushMatrix();
  196.  
  197.     glTranslatef(4.0,4.0,0.0);
  198.     glRotatef (ax,1.0,0.0,0.0);       /* animation angles */
  199.     glRotatef (-ay,0.0, 1.0, 0.0);
  200.     glRotatef (az,0.0, 0.0, 1.0);
  201.     glTranslatef(-4.0,-4.0,0.0);
  202.  
  203.     /* render the 8 triangles */
  204.     for (i=0;i<NTRI;i++) {
  205.         glColor3fv(starcolor[i]);
  206.         if (doEdgeFlag) {
  207.         glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  208.         glBegin(GL_POLYGON);
  209.         }
  210.         else {
  211.             glBegin(GL_LINE_LOOP);
  212.         }
  213.     if (doEdgeFlag) glEdgeFlag(stare[i][0]);
  214.     glVertex3fv(starv[stari[i][0]]);
  215.     if (doEdgeFlag) glEdgeFlag(stare[i][1]);
  216.     glVertex3fv(starv[stari[i][1]]);
  217.     if (doEdgeFlag) glEdgeFlag(stare[i][2]);
  218.     glVertex3fv(starv[stari[i][2]]);
  219.     glEnd();
  220.     }
  221.  
  222.     glPopMatrix();
  223.  
  224.     glFlush();
  225.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  226.  
  227. }
  228.  
  229. void
  230. setMatrix(void)
  231. {
  232.     glMatrixMode(GL_PROJECTION);
  233.     glLoadIdentity();
  234.     glOrtho(-0.5,9.5,-0.5,9.5,-10.0,10.0);
  235.     glMatrixMode(GL_MODELVIEW);
  236.     glLoadIdentity();
  237. }
  238.  
  239. void
  240. animation(void)
  241. {
  242.     register int i;
  243.     /* animate the star */
  244.  
  245.     for (i=0;i<60;i++) {
  246.     /*
  247.         ax += 5.0;
  248.         ay -= 2.0;
  249.     */
  250.     az += 3.0;
  251.         if (ax >= 360)  ax = 0.0;
  252.         if (ay <= -360) ay = 0.0;
  253.         if (az >= 360)  az = 0.0;
  254.         drawScene();
  255.     }
  256. }
  257.  
  258. void 
  259. inputCB(Widget w, XtPointer client_data, XtPointer call)
  260. {
  261.     char            string[31];
  262.     XComposeStatus  composeStatus;
  263.     KeySym keysym;
  264.     GLwDrawingAreaCallbackStruct *call_data;
  265.  
  266.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  267.  
  268.     switch(call_data->event->type) {
  269.     case ButtonPress:
  270.         switch (call_data->event->xbutton.button) {
  271.         case Button1:
  272.         animation();
  273.             break;
  274.         case Button2 :
  275.         doEdgeFlag = 0;
  276.         XtVaSetValues(toplevel,XmNtitle, "Edge Flag OFF",NULL);
  277.         drawScene();
  278.             break;
  279.         case Button3 :
  280.         doEdgeFlag = 1;
  281.         XtVaSetValues(toplevel,XmNtitle, "Edge Flag ON",NULL);
  282.         drawScene();
  283.             break;
  284.         }
  285.         break;
  286.     case KeyPress :
  287.         XLookupString((XKeyEvent *)call_data->event,string,
  288.                       30,&keysym,&composeStatus);
  289.         switch(keysym) {
  290.         case XK_Escape :
  291.             exit(0);
  292.             break;
  293.         }
  294.     break;
  295.     default:
  296.         break;
  297.     }
  298. }
  299.  
  300. void 
  301. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  302. {
  303.     GLwDrawingAreaCallbackStruct *call_data;
  304.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  305.  
  306.  
  307.     GLwDrawingAreaMakeCurrent(w, glxc);
  308.     glViewport(0, 0, call_data->width, call_data->height);
  309.     setMatrix();
  310.     drawScene();
  311. }
  312.  
  313. void 
  314. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  315. {
  316.     GLwDrawingAreaCallbackStruct *call_data;
  317.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  318.  
  319.  
  320.     GLwDrawingAreaMakeCurrent(w, glxc);
  321.     glViewport(0, 0, call_data->width, call_data->height);
  322.     drawScene();
  323. }
  324.  
  325. void
  326. initCB(Widget w, XtPointer client_data, XtPointer call)
  327. {
  328.     XVisualInfo *vi;
  329.  
  330.  
  331.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  332.     XtGetValues(w, args, 1);
  333.  
  334.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  335.  
  336.     ax = 10.0;
  337.     ay = -10.0;
  338.     az = 0.0;
  339. }
  340.